export const tcPushStream = async () => {
    const pc = new RTCPeerConnection()
    // @ts-ignore
    const stream = await navigator.mediaDevices.getDisplayMedia({
        audio: {},
        video: {
            width: 1920,
            height: 1080,
            frameRate: 24
        }
    })
    // const stream = await navigator.mediaDevices.getUserMedia({
    //     video: {
    //         width: 1280,
    //         height: 720,
    //         frameRate: 30
    //     }
    // })
    stream.getTracks().forEach(function (track) {
        pc.addTrack(track)
    })
    const offer = await pc.createOffer();
    await pc.setLocalDescription(offer);
    const apiUrl = 'https://webrtcpush.myqcloud.com/webrtc/v1/pushstream'
    const pushUrl = 'webrtc://push.daftech.cn/live/as?txSecret=35b3f96851bf41301b4d1dce70f427ad&txTime=621EEC48'
    const data = {
        "streamurl": pushUrl,
        "sessionid": "as_e1c90b41db854933803157a9b8419f26",
        "localsdp": offer
    }

    const session = await ((await fetch(apiUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
    })).json() )

    console.log(session)
    if (session.remotesdp) {
        await pc.setRemoteDescription(new RTCSessionDescription(session.remotesdp))
    }
}


export const aliPushStream = async (mediaElement) => {
    // @ts-ignore
    const pc = new RTCPeerConnection()

    // @ts-ignore
    const stream = await navigator.mediaDevices.getDisplayMedia({
        video: {
            width: 1280,
            height: 720,
            frameRate: 3
        },
        audio: true
    })

    mediaElement.srcObject = stream
    stream.getTracks().forEach(function (track) {
        pc.addTrack(track)
    })
    const offer = await pc.createOffer();
    await pc.setLocalDescription(offer);
    const pushUrl = 'artc://alipush.daftech.cn/live/as?auth_key=1645755421-0-0-e30871fdd85a6ce456b5c2646bfa3af2'
    const apiUrl = pushUrl.replace('artc', 'https')
    const data = {
        "version": 2,
        "sdk_version": "2.0.2",
        "mode": 'rtc',
        "push_stream": pushUrl,
        "jsep": offer
    }

    const session = await ((await fetch(apiUrl, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        },
        body: JSON.stringify(data),
    })).json() )

    console.log(session)
    if (session.code === 0) {
        await pc.setRemoteDescription(new RTCSessionDescription(session.jsep))
    }
}

export const aliSdkPushStream = async (AliRTS, mediaElement) => {

    const aliRts = AliRTS.createClient()
    /**
     * 获取本地流localStream
     */
    const localStream = await AliRTS.createStream({
        audio: true,
        // video: true,
        screen: true,
    })

    localStream.play(mediaElement)

    const url = 'artc://alipush.daftech.cn/live/as?auth_key=1645755421-0-0-e30871fdd85a6ce456b5c2646bfa3af2'
    aliRts.publish(url, localStream).then(() => {
        console.log('push success')
    }).catch((err) => {
        console.log('push fail')
    })
}
